package com.clw.template.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clw.template.dto.SysRoleParam;
import com.clw.template.entity.SysRole;
import com.clw.template.service.ISysRoleService;
import com.clw.template.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author clw
 * @since 2019-11-02
 */
@Api(tags = "SysRoleController",description = "后台用户角色表")
@RestController
@RequestMapping("/admin/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation("查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "分页条数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "pageNum",value = "分页页数",dataType = "Integer",paramType = "query")
    })
    @PreAuthorize("hasAuthority('sys:role:list')")
    @GetMapping("list")
    public CommonResult<IPage<SysRole>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<SysRole> page = new Page<>(pageNum,pageSize);
        return CommonResult.success(sysRoleService.page(page));
    }

    @ApiOperation("查询所有角色列表")
    @PreAuthorize("hasAuthority('sys:role:allList')")
    @GetMapping("allList")
    public CommonResult<List<SysRole>> allList(){
        return CommonResult.success(sysRoleService.list());
    }

    @PreAuthorize("hasAuthority('sys:role:getById')")
    @ApiOperation("获取指定角色")
    @GetMapping("{id}")
    public CommonResult<SysRole> getById(@PathVariable Long id){
        return CommonResult.success(sysRoleService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:role:add')")
    @ApiOperation("添加角色")
    @PostMapping("add")
    public CommonResult<SysRole> add(@Validated @RequestBody SysRoleParam roleParam){
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleParam,sysRole);
        sysRole.setCreateTime(LocalDateTime.now());
        sysRole.setUpdateTime(sysRole.getCreateTime());
        sysRoleService.save(sysRole);
        return CommonResult.success();
    }

    @PreAuthorize("hasAuthority('sys:role:deleted')")
    @ApiOperation("删除角色")
    @DeleteMapping("{id}")
    public CommonResult<SysRole> deleted(@PathVariable Long id){
        return sysRoleService.deleted(id);
    }

    @PreAuthorize("hasAuthority('sys:role:edit')")
    @ApiOperation("修改角色")
    @PutMapping("{id}")
    public CommonResult edit(@PathVariable Long id,@Validated @RequestBody SysRoleParam roleParam){
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleParam,sysRole);
        sysRole.setId(id);
        sysRole.setUpdateTime(LocalDateTime.now());
        sysRoleService.updateById(sysRole);
        return CommonResult.success();
    }

}
